package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.ScoreAdapter;
import com.wnhz.work.jy.bean.ScoreBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---积分兑换
 */
@ContentView(R.layout.activity_score_conversion)
public class ScoreConversionActivity extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.view_main_middle_title)
    private TextView title;
    @ViewInject(R.id.lv_score)
    private ListView listView;

    @ViewInject(R.id.include_occupied)
    private View include_occupied;
    @ViewInject(R.id.blank_img)
    private ImageView blank_img;
    @ViewInject(R.id.blank_tv)
    private TextView blank_tv;

    private ScoreAdapter mAdapter;
    private List<ScoreBean> scoreList = new ArrayList<>();
    private TextView tv_my_number;
    private String jindou;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    tv_my_number.setText(jindou);
                    mAdapter.notifyDataSetChanged();
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_score_conversion);
        x.view().inject(this);

        initData();
        initView();
        setBlank();
    }

    private void setBlank() {
        if (scoreList.size() == 0) {
            blank_tv.setText("积分商品上架中~");
            blank_img.setImageResource(R.mipmap.ic_kongjifenduihuan);
            include_occupied.setVisibility(View.VISIBLE);
        } else {
            include_occupied.setVisibility(View.GONE);
        }
    }

    private void initData() {
        uploadByxUtils();
    }

    private void initView() {
        //title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("金豆兑换");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.rl_integral).setOnClickListener(this);
        findViewById(R.id.rl_integral).setOnClickListener(this);

        tv_my_number = (TextView) findViewById(R.id.tv_my_number);
        //listView = (ListView) findViewById(R.id.lv_score);

        mAdapter = new ScoreAdapter(ScoreConversionActivity.this, scoreList, this);
        listView.setAdapter(mAdapter);

    }

    private void uploadByxUtils() {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        showDialog();
        XUtil.Post(ConfigUtil.INTEGRAL_EXCHANGE_LIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("ConfigUtil888" + result);
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    jindou = jsonObject.optString("jindou");
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        ScoreBean bean = new ScoreBean();
                        jsonObject = jsonArray.optJSONObject(i);
                        String goodId = jsonObject.optString("goodId");
                        String goodName = jsonObject.optString("goodName");
                        String integral = jsonObject.optString("integral");
                        String goodImg = jsonObject.optString("goodImg");
                        String num = jsonObject.optString("num");

                        bean.setGoodId(goodId);
                        bean.setImage(goodImg);
                        bean.setTitle(goodName);
                        bean.setNumber(num);
                        bean.setBeans(integral);

                        scoreList.add(bean);
                    }
                    System.out.println("ConfigUtil888" + scoreList.size());
                    setBlank();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                mHandler.sendEmptyMessage(1);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //积分明细
            case R.id.rl_integral:
                startActivity(new Intent(ScoreConversionActivity.this,CreditsExchange.class));

                break;

            default:
                break;
        }
    }
}
